<template>
  <el-dialog
    :title="!dataForm.tId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户编号" prop="ttId">
      <el-input v-model="dataForm.ttId" placeholder="用户编号"></el-input>
    </el-form-item>
    <el-form-item label="用户编号" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户编号"></el-input>
    </el-form-item>
    <el-form-item label="回复次数" prop="replyCount">
      <el-input v-model="dataForm.replyCount" placeholder="回复次数"></el-input>
    </el-form-item>
    <el-form-item label="主贴表情" prop="tEmotion">
      <el-input v-model="dataForm.tEmotion" placeholder="主贴表情"></el-input>
    </el-form-item>
    <el-form-item label="主贴标题" prop="tTopic">
      <el-input v-model="dataForm.tTopic" placeholder="主贴标题"></el-input>
    </el-form-item>
    <el-form-item label="主贴内容" prop="tContents">
      <el-input v-model="dataForm.tContents" placeholder="主贴内容"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="点击次数" prop="clickCount">
      <el-input v-model="dataForm.clickCount" placeholder="点击次数"></el-input>
    </el-form-item>
    <el-form-item label="最后点击时间" prop="lastClickTime">
      <el-input v-model="dataForm.lastClickTime" placeholder="最后点击时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          tId: 0,
          ttId: '',
          userId: '',
          replyCount: '',
          tEmotion: '',
          tTopic: '',
          tContents: '',
          createTime: '',
          clickCount: '',
          lastClickTime: ''
        },
        dataRule: {
          ttId: [
            { required: true, message: '用户编号不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户编号不能为空', trigger: 'blur' }
          ],
          replyCount: [
            { required: true, message: '回复次数不能为空', trigger: 'blur' }
          ],
          tEmotion: [
            { required: true, message: '主贴表情不能为空', trigger: 'blur' }
          ],
          tTopic: [
            { required: true, message: '主贴标题不能为空', trigger: 'blur' }
          ],
          tContents: [
            { required: true, message: '主贴内容不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          clickCount: [
            { required: true, message: '点击次数不能为空', trigger: 'blur' }
          ],
          lastClickTime: [
            { required: true, message: '最后点击时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.tId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.tId) {
            this.$http({
              url: this.$http.adornUrl(`/bbl/topic/info/${this.dataForm.tId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.ttId = data.topic.ttId
                this.dataForm.userId = data.topic.userId
                this.dataForm.replyCount = data.topic.replyCount
                this.dataForm.tEmotion = data.topic.tEmotion
                this.dataForm.tTopic = data.topic.tTopic
                this.dataForm.tContents = data.topic.tContents
                this.dataForm.createTime = data.topic.createTime
                this.dataForm.clickCount = data.topic.clickCount
                this.dataForm.lastClickTime = data.topic.lastClickTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bbl/topic/${!this.dataForm.tId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'tId': this.dataForm.tId || undefined,
                'ttId': this.dataForm.ttId,
                'userId': this.dataForm.userId,
                'replyCount': this.dataForm.replyCount,
                'tEmotion': this.dataForm.tEmotion,
                'tTopic': this.dataForm.tTopic,
                'tContents': this.dataForm.tContents,
                'createTime': this.dataForm.createTime,
                'clickCount': this.dataForm.clickCount,
                'lastClickTime': this.dataForm.lastClickTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
